package com.example.controll;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Content;
import com.example.entity.Course;
import com.example.entity.Grade;
import com.example.entity.Major;
import com.example.entity.Score;
import com.example.entity.Teacher;
import com.example.service.ContentService;
import com.example.service.CourseService;
import com.example.service.GradeService;
import com.example.service.MajorService;
import com.example.service.ScoreService;
import com.example.service.TeacherService;

@RestController // 就是返回一个json对象
@ComponentScan(basePackages = { "com.entity", "com.repository" })
public class Controller {
	// 取得service的实例接口

	@Autowired
	private MajorService majorService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private ContentService contentService;
	@Autowired
	private ScoreService scoreService;
	@Autowired
	private GradeService gradeService;

	@RequestMapping("/getMajor")
	public String getMojar(HttpServletRequest req, HttpServletResponse rep) {
		String st = "[";
		List<Major> list = majorService.getMajor();
		for (Major m : list) {
			st += "{\"id\":" + m.getId() + "," + "\"name\":\"" + m.getName() + "\"},";
		}
		String sr = st.substring(0, st.length() - 1);
		sr += "]";

		return sr;
	}

	@RequestMapping("/getCourse")
	public List<Course> getCourse(HttpServletRequest req, HttpServletResponse rep) {
		// String st = "[";
		List<Course> list = courseService.getCourse();
		// for (Course c : list) {
		// st+="{\"id\":"+c.getId()+","+"\"name\":\""+c.getName()+"\"},";
		// }
		// String sr = st.substring(0, st.length()-1);
		// sr+="]";
		return list;
	}

	@RequestMapping("/getTeacher")
	public List<Teacher> getTeacher(HttpServletRequest req, HttpServletResponse rep) {
		List<Teacher> list = teacherService.getTeacher();
		return list;
	}

	@RequestMapping("/getContent")
	public List<Content> getContent(HttpServletRequest req, HttpServletResponse rep) {
		List<Content> list = contentService.getItem();
		return list;
	}

	@RequestMapping("/getScore")
	public List<Score> getScore(HttpServletRequest req, HttpServletResponse rep) {
		List<Score> list = scoreService.getScore();
		return list;
	}

	@RequestMapping("/setForm")
	public void setForm(HttpServletRequest req, HttpServletResponse rep, @RequestParam("major") String major,
			@RequestParam("course") String course, @RequestParam("teacher") String teacher,
			@RequestParam("scores") String scores) {//通过前端包装的json直接得到数据
		System.out.println(major + "\t" + course + "\t" + teacher + "\t" + scores + "\t");

		Teacher tea = teacherService.getName(teacher);//实例化对应的对象，teacher
		Course cour = courseService.getID(course);

		String[] scoreArr = scores.split(",");//得到的分数是个数组，将其处理成string数组
		int j = 0;
		for (int i = 0; i < scoreArr.length; i++) {
			j++;//j相当于score的id

			Integer score = Integer.valueOf(scoreArr[i]);//将string数组循环并包装成int类型
			Score scoreObj = scoreService.getId(score);//得到一个score的对象（保存grade的时候方法要求是需要对象保存）
			Content content = contentService.getID(j);//通过分数的序号id得到项目的条目（前段的项目条目与分数一致）

			Grade grade = new Grade();
			//实例化评分系统表格
			grade.setContent(content);
			grade.setCourse(cour);
			grade.setDate(new Date());//保存时间对象
			grade.setScore(scoreObj);
			grade.setTeacher(tea);
            //保存对应的对象
			gradeService.setGrade(grade);
			//gradeservice保存grade对象
		}

	}
}
